package cn.stronglink.asset.module.contract.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Contract;
import cn.stronglink.asset.module.contract.ContractService;
import cn.stronglink.asset.module.contract.vo.ContractNoSelectVo;
import cn.stronglink.asset.module.contract.vo.ContractVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "contract")
@SuppressWarnings("unchecked")
public class ContractController extends AbstractController{

	@Autowired
	private ContractService contractservice;
	
	/**
	 * 列表信息查询
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		
		Page<ContractVo> page = (Page<ContractVo>) super.getPage(params);
		Page<ContractVo> data = contractservice.queryList(page, params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 创建合同信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param contract
	 * @return
	 */
	@OperateLog(module = "合同管理",desc="添加合同", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Contract contract) {
		//合同编号是佛已经存在
		Integer isExist=contractservice.getIsExist(contract);
		if(isExist>0){
			return setModelMap(modelMap, HttpCode.CONFLICT, "合同编码已经存在");
		}
		contractservice.create(contract);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 更新合同信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param contract
	 * @return
	 */
	@OperateLog(module = "合同管理",desc="修改合同", type = OpType.UPDATE)
	@RequestMapping(value = "update")
	public ResponseEntity<ModelMap> update(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Contract contract) {
		Integer isExist=contractservice.getIsExist(contract);
		if(isExist>0){
			return setModelMap(modelMap, HttpCode.CONFLICT, "合同编码已经存在");
		}
		contractservice.update(contract);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 通过id删除
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "合同管理",desc="删除合同", type = OpType.DEL)
	@RequestMapping(value = "deleteById")
	public ResponseEntity<ModelMap> deleteById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		contractservice.deleteById(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 批量删除
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param ids
	 * @return
	 */
	@OperateLog(module = "合同管理",desc="删除合同", type = OpType.DEL)
	@RequestMapping(value = "deleteByIds")
	public ResponseEntity<ModelMap> deleteByIds(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody List<Long> ids) {
		contractservice.deleteByIds(ids);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 获取合同信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getConcatInfo")
	public ResponseEntity<ModelMap> getConcatInfo(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		Contract data=contractservice.getContractInfo(id);
		return setSuccessModelMap(modelMap, data);
	}
	
	
    /**	 
	 * 查询所有合同的合同号
	 * @param modelMap
	 * @param request
	 * @param response
     * @return
   */	 
    @RequestMapping(value = "queryAllContractList")	 
	public ResponseEntity<ModelMap> queryAllContractList(ModelMap modelMap, HttpServletRequest request,	 
		HttpServletResponse response) {			 
		List<ContractNoSelectVo> data = contractservice.queryAllContractList();	 
		return setSuccessModelMap(modelMap, data);
 
	}
}
